﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;



namespace WoSoft.PACS.Data.DBProvider
{
    /// <summary>
    /// Oracle帮助类
    /// </summary>
    public class SqlServerHelper : IDisposable
    {
        private SqlConnection _connection;
        private const string CONSTR = @"Server={0}\{1};database={2};uid={3};pwd={4}";


        public SqlServerHelper(string serverip, string instance, string dbName, string username, string passwod)
        {
            var constr = string.Format(CONSTR, serverip, instance, dbName, username, passwod);
            _connection = new SqlConnection(constr);
        }

        //查询并返回结果
        public DataTable QueryTable(string sqlText, params IDataParameter[] commandParameters)
        {
            if (_connection.State != ConnectionState.Open)
            {
                _connection.Open();
            }
            var ds = new DataSet();
            SqlCommand cmd = new SqlCommand(sqlText, _connection);
            cmd.CommandType = CommandType.Text;
            if (commandParameters != null)
            {
                cmd.Parameters.AddRange(commandParameters);
            }

            using (SqlDataAdapter da = new SqlDataAdapter(cmd))
            {
                da.Fill(ds);
            }

            if (ds != null && ds.Tables.Count > 0)
            {
                return ds.Tables[0];
            }

            return null;
        }

        /// <summary>
        /// 释放连接对象
        /// </summary>
        public void Dispose()
        {
            if (_connection != null)
            {
                if (_connection.State == System.Data.ConnectionState.Open)
                {
                    _connection.Close();
                }
            }
        }
    }
}
